# -*- codeing = utf-8 -*-
# @Time: 2023/6/15 9:42
# @Author: caoyi
# @File: goods_api.py
# @Software: PyCharm
from tests.litemall_api.base_api import BaseApi, BASE_URL


class GoodsApi(BaseApi):

    def add_goods(self, goods_number, goods_name):
        # 商品添加功能
        # goods_number = self.pre + self.nowtime
        # goods_name = self.pre + self.nowtime + "冰墩墩"
        add_good_json = {
            "goods": {
                "picUrl": "",
                "gallery": [],
                "isHot": False,
                "isNew": True,
                "isOnSale": True,
                "goodsSn": goods_number,
                "name": goods_name
            },
            "specifications": [{
                "specification": "规格",
                "value": "标准",
                "picUrl": ""
            }],
            "products": [{
                "id": 0,
                "specifications": ["标准"],
                "price": 0,
                "number": 0,
                "url": ""
            }],
            "attributes": []
        }
        add_good_url = f"{BASE_URL}/admin/goods/create"
        # add_goods_resp = requests.request("post",add_good_url,headers=self.headers,json=add_good_json,proxies=proxys,verify=False)
        add_goods_resp = self.myrequest("post", add_good_url, json=add_good_json)
        return add_goods_resp

    def search_goods(self, goods_name):
        # 查询商品功能
        search_goods_url = f"{BASE_URL}/admin/goods/list?page=1&limit=20&name={goods_name}&sort=add_time&order=desc"
        # search_goods_resp = requests.request("get",search_goods_url,headers=self.headers,proxies=proxys,verify=False)
        search_goods_resp = self.myrequest("get", search_goods_url)
        return search_goods_resp

    def get_goods(self,goods_id):
        # 获得商品的json数据
        get_goods_url = f"{BASE_URL}/admin/goods/detail?id={goods_id}"
        get_goods_resp = self.myrequest("get", get_goods_url)
        return get_goods_resp
    def edit_goods(self,data,goods_names, goods_counterPrices):
        # 编辑商品功能
        edit_goods_json = data['data']
        # 注意，直接修改，直接赋值相当于修改
        edit_goods_json['goods']['name'] = goods_names
        edit_goods_json['goods']['counterPrice'] = goods_counterPrices
        edit_goods_url = f"{BASE_URL}/admin/goods/update/"
        edit_goods_resp = self.myrequest("post", edit_goods_url, json=edit_goods_json)
        return edit_goods_resp

    def del_goods(self,goods_id):
        # 删除商品功能
        del_goods_json = {
            "id":goods_id
        }
        del_goods_url = f"{BASE_URL}/admin/goods/delete/"
        del_goods_resp = self.myrequest("post",del_goods_url,json=del_goods_json)
        return del_goods_resp